package ch.bfh.abcmmh.view.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;

import ch.bfh.abcmmh.controller.operating.LogicController;
import ch.bfh.abcmmh.view.GUIController;

public class StartupFrame {

	private LogicController lctrl;
	private GUIController gctrl;
	private JFrame frmMain;
	private JButton btuStart;
	private JLabel lblInfo;
	private JTextField txtStreet;
	private int size = 460;
	private JCheckBox entranceRampCheckBox;

	public StartupFrame(LogicController lctrl, GUIController gctrl) {
		this.lctrl = lctrl;
		this.gctrl = gctrl;
		frmMain = new JFrame();
		frmMain.setSize(new Dimension(size, size));
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - size) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - size) / 2;
		frmMain.setLocation(x, y);
		frmMain.setResizable(false);
		frmMain.setAlwaysOnTop(true);
		frmMain.setUndecorated(true);
		frmMain.getRootPane().setBorder(new LineBorder(Color.BLACK));
		frmMain.getRootPane().setDefaultButton(btuStart);
		frmMain.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke("ENTER"), "none");
		frmMain.setLayout(new BorderLayout());
		frmMain.add(new TitlePanel(), BorderLayout.NORTH);
		frmMain.add(new LayoutHelper(), BorderLayout.CENTER);
		frmMain.add(new ButtonPanel(), BorderLayout.SOUTH);
		frmMain.getContentPane().setForeground(Color.BLUE);
		frmMain.setVisible(true);
		frmMain.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (KeyEvent.VK_ENTER == e.getKeyCode()) {
					actionEvent();
				}
			}
		});
	}

	private void run() {
		lctrl.setCountOfStreets(getNumOfStreets());
		lctrl.setupLogic();
		gctrl.setupGui();
	}

	private int getNumOfStreets() {
		try {
			return Integer.parseInt(txtStreet.getText());
		} catch (Exception e) {
			return 3;
		}
	}

	private class TitlePanel extends JPanel {
		private static final long serialVersionUID = 1L;

		private TitlePanel() {
			this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			JLabel lblMain = new JLabel("TrafficSim");
			lblMain.setFont(new Font("Arial", Font.PLAIN, 44));
			lblMain.setAlignmentX(TitlePanel.CENTER_ALIGNMENT);
			this.add(lblMain);
			JLabel lblSub = new JLabel("A simple java based traffic simulator");
			lblSub.setFont(new Font("Arial", Font.PLAIN, 16));
			lblSub.setAlignmentX(TitlePanel.CENTER_ALIGNMENT);
			this.add(lblSub);
			JLabel lblImg = new JLabel(new ImageIcon(getClass().getResource(
					"/traffic.gif")));
			lblImg.setAlignmentX(TitlePanel.CENTER_ALIGNMENT);
			this.add(lblImg);
		}
	}

	private class InputPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		private InputPanel() {
			// this.setLayout(new FlowLayout());
			this.setLayout(new GridLayout(2, 1));
			txtStreet = new JTextField("5", 2);
			entranceRampCheckBox = new JCheckBox();
			entranceRampCheckBox.setSelected(false);
			txtStreet.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					if (KeyEvent.VK_ENTER == e.getKeyCode()) {
						actionEvent();
					}
				}
			});
			this.add(new InputpanelFormater(new JLabel(
					"Number of streets to simulate: "), txtStreet));
			this.add(new InputpanelFormater(new JLabel(
					"Street has a freeway entrance ramp: "),
					entranceRampCheckBox));
		}
	}

	private class ButtonPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		private ButtonPanel() {
			this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			btuStart = new JButton("Start Simulation");
			btuStart.addActionListener(new StartActionListener());
			btuStart.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					actionEvent();
				}
			});
			btuStart.setAlignmentX(TitlePanel.CENTER_ALIGNMENT);
			this.add(btuStart);
			lblInfo = new JLabel(" ");
			lblInfo.setForeground(Color.RED);
			lblInfo.setAlignmentX(TitlePanel.CENTER_ALIGNMENT);
			this.add(lblInfo);
		}
	}

	private class LayoutHelper extends JPanel {

		private static final long serialVersionUID = 1L;

		private LayoutHelper() {
			setLayout(new FlowLayout());
			add(new InputPanel());
		}
	}

	private class InputpanelFormater extends JPanel {

		private static final long serialVersionUID = 1L;

		private InputpanelFormater(JComponent c1, JComponent c2) {
			setLayout(new BorderLayout());
			add(c1, BorderLayout.WEST);
			add(c2, BorderLayout.EAST);
		}
	}

	public void actionEvent() {
		int streets = getNumOfStreets();
		if (streets >= 1 && streets < 6) {
			boolean cb = entranceRampCheckBox.isSelected();
			if (cb && streets <3) {				
				lblInfo.setText("at least 3 Streets are needed for a entrance ramp");
				return;
			}
			frmMain.setVisible(false);
			lctrl.setEntranceRamp(cb);
			run();
		} else {
			lblInfo.setText("The simulator accpets only values between 1 and 5");
		}
	}

	private class StartActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			actionEvent();
		}

	}

	public JCheckBox getEntranceRampCheckBox() {
		return entranceRampCheckBox;
	}

}